package com.jet.rtdb.service.impl;

import com.jet.rtdb.elasticsearch.eneity.Electric;
import com.jet.rtdb.elasticsearch.repository.ESRepository;
import com.jet.rtdb.pojo.QueryHistoryData;
import com.jet.rtdb.pojo.QueryHistoryDataList;
import com.jet.rtdb.pojo.QueryLastData;
import com.jet.rtdb.pojo.QueryLastDataList;
import com.jet.rtdb.service.RtdbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhangkaifeng.
 * @CreateTime: 2017/8/22 14:26
 * @Description:
 */

@Service
public class RtdbServiceImpl implements RtdbService{

    @Autowired
    private ESRepository esRepository;

    @Override
    public QueryLastDataList getLastData(QueryLastDataList queryLastDataList) {
        for (int i = 0; i < queryLastDataList.size(); i++) {
            QueryLastData queryLastData = queryLastDataList.get(i);
            Electric electric = esRepository.queryLastData(queryLastData.getSiteName(), queryLastData.getPointName());
            queryLastData.setResultTime(electric.getDateTime().getTime());
            queryLastData.setResultValue(electric.getValue());
            queryLastData.setResultCode(1);
        }
        return queryLastDataList;
    }

    @Override
    public QueryHistoryDataList getHistoryData(QueryHistoryDataList queryHistoryDataList) {
        for (int i = 0; i < queryHistoryDataList.size(); i++) {
            QueryHistoryData queryHistoryData = queryHistoryDataList.get(i);
            List<Double> valueList = new ArrayList<>();
            String siteName = queryHistoryData.getSiteName();
            String opName = queryHistoryData.getPointName();
            List<Long> timeList = queryHistoryData.getResultTimeList();
            for (int j = 0; j < timeList.size(); j++) {
                Electric electric = esRepository.queryByTime(siteName, opName, timeList.get(j), 15);
                valueList.add(electric.getValue());
            }
            queryHistoryData.setResultCode(1);
            queryHistoryData.setResultValueList(valueList);
        }
        return queryHistoryDataList;
    }
}
